///////////////////////////////////////////////////////////////////////////////
//-----------------------------------------------------------------------------
#include <Scripting/Script.h>
#include <File/LocalFile.h>
#include <Scene/Object.h>

#define	HANDLE_ERROR(X)		{ int err = (X); if( err ) HandleError( err ); }

//-----------------------------------------------------------------------------

Script::Script()
{
	m_state = NULL;
}

//-----------------------------------------------------------------------------

Script::~Script()
{
	Release();
}

//-----------------------------------------------------------------------------

void		Script::Release()
{
	if( m_state )
	{
		lua_close( m_state );
		m_state = NULL;
	}
}

//-----------------------------------------------------------------------------

bool		Script::Load( std::string name )
{
	//LocalFile*	file = MGL_NEW LocalFile();
	//file->Open( name.c_str() );
	//if( !file->IsOpen() )
	//{
	//	SAFE_DEL( file );
	//	return false;
	//}
	////read in a buffer, do stuff with buffer if needed and then use luaL_loadstring()

	if( !LocalFile::Exists( name.c_str() ) )
	{
		return false;
	}
	Release();

	m_state = luaL_newstate();

	luaopen_io( m_state );
	luaopen_base( m_state );
	luaopen_table( m_state );
	luaopen_string( m_state );
	luaopen_math( m_state );

	HANDLE_ERROR( luaL_loadfile( m_state, name.c_str() ) );

	return true;
}

//-----------------------------------------------------------------------------

void		Script::RunScript()
{
	if( m_state )
	{
		HANDLE_ERROR( lua_pcall( m_state, 0, LUA_MULTRET, 0 ) );
	}
}

//-----------------------------------------------------------------------------

void		Script::RegisterFunction( std::string name, lua_CFunction func )
{
	if( m_state )
	{
		lua_register( m_state, name.c_str() , func );
	}
}

//-----------------------------------------------------------------------------

void		Script::RunFunction( std::string name )
{
	if( m_state )
	{
		lua_getglobal( m_state, name.c_str() );
		HANDLE_ERROR( lua_pcall( m_state, 0, 0, 0 ) );
	}
}

//-----------------------------------------------------------------------------

void		Script::RunFunction( std::string name, Object* obj )
{
	if( m_state )
	{
		lua_getglobal( m_state, name.c_str() );
		lua_pushnumber( m_state, (int)obj );
		HANDLE_ERROR( lua_pcall( m_state, 1, 0, 0 ) );
	}
}

//-----------------------------------------------------------------------------

bool		Script::IsThisScript( std::string name )
{
	return m_name == name;
}

//-----------------------------------------------------------------------------

void		Script::HandleError( int err )
{
	if( err )
	{
		DEBUG_ERR( "[Script] Error: %s\n", lua_tostring( m_state, -1 ) );
		lua_pop( m_state, 1 );
	}
}

//-----------------------------------------------------------------------------
///////////////////////////////////////////////////////////////////////////////
